﻿using System;
using System.Data;
using System.Data.SqlClient;
using Company.BLL.WS.KDT.SXXK;
using Company.BLL.WS.SXXK;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Xml;
namespace Company.BLL.KDT.SXXK
{
    public partial class HoSoThanhLyDangKy
    {
        private BangKeHoSoThanhLyCollection _BKCollection = new BangKeHoSoThanhLyCollection();

        public BangKeHoSoThanhLyCollection BKCollection
        {
            set { this._BKCollection = value; }
            get { return this._BKCollection; }
        }
        public int getBKToKhaiXuat()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLTKX")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKToKhaiNhap()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLTKN")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLChuaThanhLY()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLCHUATL")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLXinHuy()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLXH")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLNopThue()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLNT")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLTamNopThue()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLCHITIETNT")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLTuCungUng()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLTCU")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLTaiXuat()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLTX")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLXuatGiaCong()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLXGC")
                {
                    return i;
                }
            }
            return -1;
        }
        public int getBKNPLNhapKinhDoanh()
        {
            for (int i = 0; i < this.BKCollection.Count; i++)
            {
                if (this.BKCollection[i].MaBangKe == "DTLNPLNKD")
                {
                    return i;
                }
            }
            return -1;
        }
        //-----------------------------------------------------------------------------------------
        public void LoadBKCollection()
        {
            this._BKCollection = BangKeHoSoThanhLy.SelectCollectionBy_MaterID(this.ID);
        }

        //-----------------------------------------------------------------------------------------

        public bool InsertUpdateFull()
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    if (this._ID == 0)
                        this._ID = this.InsertTransaction(transaction);
                    else
                        this.UpdateTransaction(transaction);

                    foreach (BangKeHoSoThanhLy bkDetail in this.BKCollection)
                    {
                        bkDetail.MaterID = this._ID;
                        bkDetail.InsertUpdateFull(transaction);
                    }
                    transaction.Commit();
                    ret = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }

        public void InsertUpdateFull(SqlTransaction transaction)
        {
            if (this._ID == 0)
                this._ID = this.InsertTransaction(transaction);
            else
                this.UpdateTransaction(transaction);

            foreach (BangKeHoSoThanhLy bkDetail in this.BKCollection)
            {
                bkDetail.MaterID = this._ID;
                bkDetail.InsertUpdateFull(transaction);
            }
        }

        public int checkHSTLHasCreated()
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            string sql = "SELECT id from t_KDT_SXXK_HoSoThanhLyDangKy WHERE TrangThaiXuLy!=1";
            DbCommand cmd = db.GetSqlStringCommand(sql);
            int id = 0;
            try
            {
                id = Convert.ToInt32(db.ExecuteScalar(cmd));
            }
            catch
            {
                id = 0;
            }
            if (id == 0)
            {
                sql = "SELECT id from t_KDT_SXXK_HoSoThanhLyDangKy WHERE TrangThaiXuLy=1";
                cmd = db.GetSqlStringCommand(sql);
                int id1 = 0;
                try
                {
                    id1 = Convert.ToInt32(db.ExecuteScalar(cmd));
                }
                catch
                {
                    id1 = 0;
                }
                if (id1 == 0) return 0;
                else
                {
                    sql = "SELECT count(*) from t_SXXK_ThanhLy_LanThanhLy WHERE TrangThai!=401";
                    cmd = db.GetSqlStringCommand(sql);
                    int count = 0;
                    try
                    {
                        count = Convert.ToInt32(db.ExecuteScalar(cmd));
                    }
                    catch
                    {
                        count = 0;
                    }
                    if (count == 0) return 0;
                    else return id1;
                }
            }
            else return id;
        }

        public DataSet KhoiTaoThanhLy()
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            //Lấy danh sách NPL tồn của bảng kê tờ khai nhập thanh lý
            try
            {
                string sp = "p_KDT_SXXK_DanhSachNPLNhapTon";
                DbCommand cmd = db.GetStoredProcCommand(sp);
                db.AddInParameter(cmd, "@BangKeHoSoThanhLy_ID", DbType.Int64, this.BKCollection[this.getBKToKhaiNhap()].ID);
                db.LoadDataSet(cmd, ds, "t_NPLNhapTon");

                sp = "p_KDT_SXXK_DanhSachNPLXuatTon";
                cmd = db.GetStoredProcCommand(sp);
                db.AddInParameter(cmd, "@BangKeHoSoThanhLy_ID", DbType.Int64, this.BKCollection[this.getBKToKhaiXuat()].ID);
                db.LoadDataSet(cmd, ds, "t_NPLXuatTon");
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return ds;

        }
        public DataSet ChayThanhLy(DataSet ds,ref BCXuatNhapTonCollection bcXNTCollection)
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    DataTable dtNPLXuatTon = ds.Tables["t_NPLXuatTon"];
                    DataTable dtNPLNhapTon = ds.Tables["t_NPLNhapTon"];

                    DataTable dtBCXuatNhapTon = new DataTable();
                    DataColumn[] cols = new DataColumn[22];
                    cols[0] = new DataColumn("LanThanhLy", typeof(int));
                    cols[1] = new DataColumn("MaDoanhNghiep", typeof(string));
                    cols[2] = new DataColumn("STT", typeof(long));
                    cols[3] = new DataColumn("MaNPL", typeof(string));
                    cols[4] = new DataColumn("SoToKhaiNhap", typeof(int));
                    cols[5] = new DataColumn("NgayDangKyNhap", typeof(DateTime));
                    cols[6] = new DataColumn("NgayHoanThanhNhap", typeof(DateTime));
                    cols[7] = new DataColumn("LuongNhap", typeof(decimal));
                    cols[8] = new DataColumn("LuongTonDau", typeof(decimal));
                    cols[9] = new DataColumn("TenDVT_NPL", typeof(string));
                    cols[10] = new DataColumn("MaSP", typeof(string));
                    cols[11] = new DataColumn("SoToKhaiXuat", typeof(int));
                    cols[12] = new DataColumn("NgayDangKyXuat", typeof(DateTime));
                    cols[13] = new DataColumn("NgayHoanThanhXuat", typeof(DateTime));
                    cols[14] = new DataColumn("LuongSPXuat", typeof(decimal));
                    cols[15] = new DataColumn("TenDVT_SP", typeof(string));
                    cols[16] = new DataColumn("DinhMuc", typeof(decimal));
                    cols[17] = new DataColumn("LuongNPLSuDung", typeof(decimal));
                    cols[18] = new DataColumn("ToKhaiTaiXuat", typeof(decimal));
                    cols[18] = new DataColumn("LuongNPLTaiXuat", typeof(decimal));
                    cols[19] = new DataColumn("LuongTonCuoi", typeof(decimal));
                    cols[20] = new DataColumn("ThanhKhoanTiep", typeof(decimal));
                    cols[21] = new DataColumn("ChuyenMucDichKhac", typeof(decimal));
                    dtBCXuatNhapTon.Columns.AddRange(cols);

                    DataTable dtBCThueXNK = new DataTable();
                    DataColumn[] cols1 = new DataColumn[22];
                    cols1[0] = new DataColumn("LanThanhLy", typeof(int));
                    cols1[1] = new DataColumn("MaDoanhNghiep", typeof(string));
                    cols1[2] = new DataColumn("STT", typeof(long));
                    cols1[3] = new DataColumn("SoToKhaiNhap", typeof(int));
                    cols1[4] = new DataColumn("NgayDangKyNhap", typeof(DateTime));
                    cols1[5] = new DataColumn("NgayThucNhap", typeof(DateTime));
                    cols1[6] = new DataColumn("MaNPL", typeof(string));
                    cols1[7] = new DataColumn("LuongNhap", typeof(decimal));
                    cols1[9] = new DataColumn("TenDVT_NPL", typeof(string));
                    cols1[10] = new DataColumn("DonGiaTT", typeof(decimal));
                    cols1[11] = new DataColumn("TyGiaTT", typeof(decimal));
                    cols1[12] = new DataColumn("ThueSuat", typeof(decimal));
                    cols1[13] = new DataColumn("ThueNKNop", typeof(decimal));
                    cols1[14] = new DataColumn("SoToKhaiXuat", typeof(int));
                    cols1[15] = new DataColumn("NgayDangKyXuat", typeof(DateTime));
                    cols1[16] = new DataColumn("NgayThucXuat", typeof(DateTime));
                    cols1[17] = new DataColumn("LuongNPLSuDung", typeof(decimal));
                    cols1[18] = new DataColumn("LuongNPLTon", typeof(decimal));
                    cols1[19] = new DataColumn("TienThueHoan", typeof(decimal));
                    cols1[20] = new DataColumn("TienThueTKTiep", typeof(decimal));
                    cols1[21] = new DataColumn("GhiChu", typeof(string));
                    dtBCThueXNK.Columns.AddRange(cols1);



                    //Thanh ly bang ke NPL xin huy
                    int index = this.getBKNPLXinHuy();
                    if (index >= 0)
                    {
                        DataColumn col = new DataColumn("LuongXH", typeof(decimal));
                        dtNPLNhapTon.Columns.Add(col);
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLXinHuy bk in this.BKCollection[index].bkNPLXHCollection)
                        {
                            foreach (DataRow dr in dtNPLNhapTon.Rows)
                            {
                                if (bk.SoToKhai == Convert.ToInt32(dr["SoToKhai"]) && bk.MaLoaiHinh == Convert.ToString(dr["MaLoaiHinh"]) && bk.NamDangKy == Convert.ToInt16(dr["NamDangKy"]) &&
                                    bk.MaHaiQuan == Convert.ToString(dr["MaHaiQuan"]) && bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    dr["TonCuoi"] = Convert.ToDecimal(dr["TonCuoi"]) - bk.LuongHuy;
                                    dr["LuongXH"] = bk.LuongHuy;
                                    break;
                                }
                            }
                        }
                    }
                    //Thanh ly bang ke NPL tai xuat
                    index = this.getBKNPLTaiXuat();
                    if (index >= 0)
                    {
                        DataColumn col = new DataColumn("LuongTX", typeof(decimal));
                        dtNPLNhapTon.Columns.Add(col);
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLTaiXuat bk in this.BKCollection[index].bkNPLTXCollection)
                        {
                            foreach (DataRow dr in dtNPLNhapTon.Rows)
                            {
                                if (bk.SoToKhai == Convert.ToInt32(dr["SoToKhai"]) && bk.MaLoaiHinh == Convert.ToString(dr["MaLoaiHinh"]) && bk.NamDangKy == Convert.ToInt16(dr["NamDangKy"]) &&
                                    bk.MaHaiQuan == Convert.ToString(dr["MaHaiQuan"]) && bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    dr["TonCuoi"] = Convert.ToDecimal(dr["TonCuoi"]) - bk.LuongTaiXuat;
                                    dr["LuongTX"] = bk.LuongTaiXuat;
                                    break;
                                }
                            }
                        }
                    }
                    //Thanh ly bang ke NPL nop thue
                    index = this.getBKNPLNopThue();
                    if (index >= 0)
                    {
                        DataColumn col = new DataColumn("LuongNT", typeof(decimal));
                        dtNPLNhapTon.Columns.Add(col);
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLNopThueTieuThuNoiDia bk in this.BKCollection[index].bkNPLNTCollection)
                        {
                            foreach (DataRow dr in dtNPLNhapTon.Rows)
                            {
                                if (bk.SoToKhai == Convert.ToInt32(dr["SoToKhai"]) && bk.MaLoaiHinh == Convert.ToString(dr["MaLoaiHinh"]) && bk.NamDangKy == Convert.ToInt16(dr["NamDangKy"]) &&
                                    bk.MaHaiQuan == Convert.ToString(dr["MaHaiQuan"]) && bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    dr["TonCuoi"] = Convert.ToDecimal(dr["TonCuoi"]) - bk.LuongNopThue;
                                    dr["LuongNT"] = bk.LuongNopThue;
                                    break;
                                }
                            }
                        }
                    }
                    //Thanh ly bang ke NPL chua thanh ly
                    index = this.getBKNPLChuaThanhLY();
                    if (index >= 0)
                    {
                        DataColumn col = new DataColumn("LuongCTL", typeof(decimal));
                        dtNPLNhapTon.Columns.Add(col);
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLChuaThanhLy bk in this.BKCollection[index].bkNPLCTLCollection)
                        {
                            foreach (DataRow dr in dtNPLNhapTon.Rows)
                            {
                                if (bk.SoToKhai == Convert.ToInt32(dr["SoToKhai"]) && bk.MaLoaiHinh == Convert.ToString(dr["MaLoaiHinh"]) && bk.NamDangKy == Convert.ToInt16(dr["NamDangKy"]) &&
                                    bk.MaHaiQuan == Convert.ToString(dr["MaHaiQuan"]) && bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    dr["TonCuoi"] = Convert.ToDecimal(dr["TonCuoi"]) - bk.Luong;
                                    dr["LuongCTL"] = bk.Luong;
                                    break;
                                }
                            }
                        }
                    }
                    index = this.getBKNPLTuCungUng();
                    if (index >= 0)
                    {
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLTuCungUng bk in this.BKCollection[index].bkNPLTCUCollection)
                        {
                            foreach (DataRow dr in dtNPLXuatTon.Rows)
                            {
                                if (bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    if (bk.LuongTuCungUng <= Convert.ToDecimal(dr["TonNPL"]))
                                    {
                                        dr["TonNPL"] = Convert.ToDecimal(dr["TonNPL"]) - bk.LuongTuCungUng;
                                        break;
                                    }
                                    else
                                    {
                                        bk.LuongTuCungUng -= Convert.ToDecimal(dr["TonNPL"]);
                                        dr["TonNPL"] = 0;
                                    }

                                }
                            }
                        }
                    }
                    //Thanh ly bang ke NPL xuat gia cong
                    index = this.getBKNPLXuatGiaCong();
                    if (index >= 0)
                    {
                        DataColumn col = new DataColumn("LuongXGC", typeof(decimal));
                        dtNPLNhapTon.Columns.Add(col);
                        this.BKCollection[index].LoadChiTietBangKe();
                        foreach (BKNPLXuatGiaCong bk in this.BKCollection[index].bkNPLXGCCollection)
                        {
                            foreach (DataRow dr in dtNPLNhapTon.Rows)
                            {
                                if (bk.SoToKhai == Convert.ToInt32(dr["SoToKhai"]) && bk.MaLoaiHinh == Convert.ToString(dr["MaLoaiHinh"]) && bk.NamDangKy == Convert.ToInt16(dr["NamDangKy"]) &&
                                    bk.MaHaiQuan == Convert.ToString(dr["MaHaiQuan"]) && bk.MaNPL == Convert.ToString(dr["MaNPL"]))
                                {
                                    dr["TonCuoi"] = Convert.ToDecimal(dr["TonCuoi"]) - bk.LuongXuat;
                                    dr["LuongXGC"] = bk.LuongXuat;
                                    break;
                                }
                            }
                        }
                    }
                    foreach (DataRow dr1 in dtNPLXuatTon.Rows)
                    {
                        int j = -1;
                        for (int i = 0; i < dtNPLNhapTon.Rows.Count; i++)
                        {
                            DataRow dr2 = dtNPLNhapTon.Rows[i];

                            if (dr2["MaNPL"].ToString().ToLower() == dr1["MaNPL"].ToString().ToLower() && Convert.ToDecimal(dr2["TonDau"]) > 0)
                            {
                                #region dtBCXuatNhapTon

                                DataRow dr = dtBCXuatNhapTon.NewRow();
                                dr["LanThanhLy"] = Company.BLL.SXXK.ThanhKhoan.LanThanhLyBase.getLanThanhLyMoiNhat(this.MaDoanhNghiep, this.MaHaiQuanTiepNhan);
                                dr["MaDoanhNghiep"] = this.MaDoanhNghiep;
                                dr["STT"] = 0;
                                dr["MaNPL"] = dr2["MaNPL"].ToString();
                                dr["SoToKhaiNhap"] = dr2["SoToKhai"].ToString();
                                dr["NgayDangKyNhap"] = Convert.ToDateTime(dr2["NgayDangKy"]);
                                dr["NgayHoanThanhNhap"] = Convert.ToDateTime(dr2["NgayThucNhap"]);
                                dr["LuongNhap"] = Convert.ToDecimal(dr2["Luong"]);
                                dr["LuongTonDau"] = Convert.ToDecimal(dr2["TonDau"]);
                                dr["TenDVT_NPL"] = Convert.ToString(dr2["TenDVT_NPL"]);
                                dr["MaSP"] = dr1["MaSP"].ToString();
                                dr["SoToKhaiXuat"] = dr1["SoToKhai"].ToString();
                                dr["NgayDangKyXuat"] = Convert.ToDateTime(dr1["NgayDangKy"]);
                                dr["NgayHoanThanhXuat"] = Convert.ToDateTime(dr1["NgayHoanThanh"]);
                                dr["LuongSPXuat"] = Convert.ToDecimal(dr1["LuongSP"]);
                                dr["TenDVT_SP"] = Convert.ToString(dr1["TenDVT_SP"]);
                                dr["DinhMuc"] = Convert.ToDecimal(dr1["DinhMuc"]);
                                dr["LuongNPLSuDung"] = Convert.ToDecimal(dr1["LuongNPL"]);
                                #endregion
                                if (Convert.ToDecimal(dr2["TonCuoi"]) > 0)
                                {
                                    if (Convert.ToDecimal(dr2["TonCuoi"]) >= Convert.ToDecimal(dr1["TonNPL"]))
                                    {
                                        dr2["TonCuoi"] = Convert.ToDecimal(dr2["TonCuoi"]) - Convert.ToDecimal(dr1["TonNPL"]);
                                        dr1["TonNPL"] = 0;
                                        dr["LuongTonCuoi"] = Convert.ToDecimal(dr2["TonCuoi"]);
                                        dtBCXuatNhapTon.Rows.Add(dr);
                                        break;
                                    }
                                    else
                                    {
                                        dr1["TonNPL"] = Convert.ToDecimal(dr1["TonNPL"]) - Convert.ToDecimal(dr2["TonCuoi"]);
                                        dr2["TonCuoi"] = 0;
                                        j = i;
                                    }
                                }
                                else
                                {
                                    j = i;
                                }
                                dr["LuongTonCuoi"] = Convert.ToDecimal(dr2["TonCuoi"]);
                                dtBCXuatNhapTon.Rows.Add(dr);
                            }
                        }
                        if (Convert.ToDecimal(dr1["TonNPL"]) > 0)
                        {
                            if (j >= 0)
                            {
                                dtNPLNhapTon.Rows[j]["TonCuoi"] = Convert.ToDecimal(dtNPLNhapTon.Rows[j]["TonCuoi"]) - Convert.ToDecimal(dr1["TonNPL"]);
                                dtBCXuatNhapTon.Rows[dtBCXuatNhapTon.Rows.Count - 1]["LuongTonCuoi"] = Convert.ToDecimal(dtNPLNhapTon.Rows[j]["TonCuoi"]);
                                dr1["TonNPL"] = 0;
                            }
                            else
                            {
                                string toKhaiXuat = dr1["SoToKhai"].ToString() + "/" + dr1["MaLoaiHinh"].ToString() + "/" + dr1["NamDangKy"].ToString();
                                if (this.getBKNPLTuCungUng() < 0)
                                    throw new Exception("Trong tờ khai xuất '" + toKhaiXuat + "' nguyên phụ liệu '" + dr1["MaNPL"].ToString() + "' quy đổi từ sản phẩm '" + dr1["MaSP"].ToString() + "' không có trong bất cứ tờ khai nhập cần thanh lý nào. Bạn cần phải kê khai tờ khai nhập nguyên phụ liệu '" + dr1["MaNPL"].ToString() + "' hoặc kê khai bảng kê nguyên phụ liệu tự cung ứng.");
                                else
                                {
                                    throw new Exception("Trong tờ khai xuất '" + toKhaiXuat + "' nguyên phụ liệu '" + dr1["MaNPL"].ToString() + "' quy đổi từ sản phẩm '" + dr1["MaSP"].ToString() + "' không có trong bất cứ tờ khai nhập cần thanh lý nào. Bạn phải tăng lượng tự cung ứng của NPL '" + dr1["MaNPL"].ToString() + "' lên thêm ít nhất " + Convert.ToDecimal(dr1["TonNPL"]) + ".");
                                }
                            }
                        }
                    }
                    dtBCXuatNhapTon.DefaultView.Sort = "NgayDangKyNhap, maNPL";
                    int STT = 0;
                    string maNPL = "";
                    int soToKhaiNhap = 0;
                    DateTime ngayDangKyNhap = new DateTime(1900, 1, 1);
                    foreach (DataRowView rv in dtBCXuatNhapTon.DefaultView)
                    {
                        if (maNPL != Convert.ToString(rv["MaNPL"]) || soToKhaiNhap != Convert.ToInt32(rv["SoToKhaiNhap"]) || ngayDangKyNhap != Convert.ToDateTime(rv["NgayDangKyNhap"])) STT++;
                        BCXuatNhapTon bc = new BCXuatNhapTon();
                        bc.STT = STT;
                        if (rv["ChuyenMucDichKhac"].ToString() != "")
                            bc.ChuyenMucDichKhac = Convert.ToDecimal(rv["ChuyenMucDichKhac"]);
                        bc.DinhMuc = Convert.ToDecimal(rv["DinhMuc"]);
                        bc.LanThanhLy = Convert.ToInt32(rv["LanThanhLy"]);
                        bc.LuongNhap = Convert.ToDecimal(rv["LuongNhap"]);
                        bc.LuongNPLSuDung = Convert.ToDecimal(rv["LuongNPLSuDung"]);
                        if (rv["LuongNPLTaiXuat"].ToString() != "")
                            bc.LuongNPLTaiXuat = Convert.ToDecimal(rv["LuongNPLTaiXuat"]);
                        bc.LuongSPXuat = Convert.ToDecimal(rv["LuongSPXuat"]);
                        bc.LuongTonCuoi = Convert.ToDecimal(rv["LuongTonCuoi"]);
                        bc.LuongTonDau = Convert.ToDecimal(rv["LuongTonDau"]);
                        bc.MaDoanhNghiep = Convert.ToString(rv["MaDoanhNghiep"]);
                        maNPL = bc.MaNPL = Convert.ToString(rv["MaNPL"]);
                        bc.MaSP = Convert.ToString(rv["MaSP"]);
                        bc.NamThanhLy = DateTime.Today.Year;
                        ngayDangKyNhap = bc.NgayDangKyNhap = Convert.ToDateTime(rv["NgayDangKyNhap"]);
                        bc.NgayDangKyXuat = Convert.ToDateTime(rv["NgayDangKyXuat"]);
                        bc.NgayHoanThanhNhap = Convert.ToDateTime(rv["NgayHoanThanhNhap"]);
                        bc.NgayHoanThanhXuat = Convert.ToDateTime(rv["NgayHoanThanhXuat"]);
                        soToKhaiNhap = bc.SoToKhaiNhap = Convert.ToInt32(rv["SoToKhaiNhap"]);
                        bc.SoToKhaiXuat = Convert.ToInt32(rv["SoToKhaiXuat"]);
                        bc.TenDVT_NPL = Convert.ToString(rv["TenDVT_NPL"]);
                        bc.TenDVT_SP = Convert.ToString(rv["TenDVT_SP"]);
                        if (rv["ThanhKhoanTiep"].ToString() != "")
                            bc.ThanhKhoanTiep = Convert.ToDecimal(rv["ThanhKhoanTiep"]);
                        bcXNTCollection.Add(bc);

                    }
                    this.InsertBCXNT(bcXNTCollection,transaction);
                    int soToKhaiXuat = 0;
                    maNPL = "";
                    soToKhaiNhap = 0;
                    ngayDangKyNhap = new DateTime(1900,1,1);
                    DateTime ngayDangKyXuat = new DateTime(1900, 1, 1);
                    BCThueXNKCollection bcThueXNKCollectin = new BCThueXNKCollection();
                    BCThueXNK bcThueXNK = new BCThueXNK(); ;
                    STT = 0;
                    foreach (BCXuatNhapTon bc in bcXNTCollection)
                    {
                        if (maNPL == bc.MaNPL && soToKhaiNhap == bc.SoToKhaiNhap && ngayDangKyNhap == bc.NgayDangKyNhap && soToKhaiXuat == bc.SoToKhaiXuat && ngayDangKyXuat == bc.NgayDangKyXuat)
                        {
                            bcThueXNK.LuongNPLTon = bc.LuongTonCuoi;
                            bcThueXNK.LuongNPLSuDung += bc.LuongNPLSuDung + bc.LuongNPLTaiXuat;
                            bcThueXNK.LuongNPLTon = bcThueXNK.LuongNhap - bcThueXNK.LuongNPLSuDung;
                            bcThueXNK.TienThueHoan = (bcThueXNK.LuongNPLSuDung / bcThueXNK.LuongNhap) * bcThueXNK.ThueNKNop;
                            bcThueXNK.TienThueTKTiep = bcThueXNK.ThueNKNop - bcThueXNK.TienThueHoan;
                            if (bcThueXNK.TienThueTKTiep > 0) bcThueXNK.GhiChu = "Chuyển lần sau TK.";
                            bcThueXNKCollectin[bcThueXNKCollectin.Count - 1] = bcThueXNK;
                        }
                        else
                        {
                            bcThueXNK = new BCThueXNK();
                            bcThueXNK.STT = bc.STT;
                            bcThueXNK.LanThanhLy = bc.LanThanhLy;
                            bcThueXNK.NamThanhLy = bc.NamThanhLy;
                            bcThueXNK.MaDoanhNghiep = bc.MaDoanhNghiep;
                            bcThueXNK.SoToKhaiNhap = bc.SoToKhaiNhap;
                            bcThueXNK.NgayDangKyNhap = bc.NgayDangKyNhap;
                            bcThueXNK.NgayThucNhap = bc.NgayHoanThanhNhap;
                            bcThueXNK.MaNPL = bc.MaNPL;
                            bcThueXNK.LuongNhap = bc.LuongTonDau;
                            bcThueXNK.TenDVT_NPL = bc.TenDVT_NPL;
                            DataRow dr = GetDonGia_TyGia_ThueSuat_ThueNK(dtNPLNhapTon, bcThueXNK.SoToKhaiNhap, bcThueXNK.NgayDangKyNhap, bcThueXNK.MaNPL);
                            bcThueXNK.DonGiaTT = Convert.ToDecimal(dr["DonGiaTT"]);
                            bcThueXNK.TyGiaTT = Convert.ToDecimal(dr["TyGiaTT"]);
                            bcThueXNK.ThueSuat = Convert.ToDecimal(dr["ThueSuat"]);
                            bcThueXNK.ThueNKNop = Convert.ToDecimal(dr["TonDauThueXNK"]);
                            bcThueXNK.SoToKhaiXuat = bc.SoToKhaiXuat;
                            bcThueXNK.NgayDangKyXuat = bc.NgayDangKyXuat;
                            bcThueXNK.NgayThucXuat = this.GetNgayThucXuat(bcThueXNK.SoToKhaiXuat, bcThueXNK.NgayDangKyXuat);
                            bcThueXNK.LuongNPLSuDung = bc.LuongNPLSuDung + bc.LuongNPLTaiXuat;
                            bcThueXNK.LuongNPLTon = bc.LuongTonCuoi;
                            bcThueXNK.TienThueHoan = (bcThueXNK.LuongNPLSuDung / bcThueXNK.LuongNhap) * bcThueXNK.ThueNKNop;
                            bcThueXNK.TienThueTKTiep = bcThueXNK.ThueNKNop - bcThueXNK.TienThueHoan;
                            if (bcThueXNK.TienThueTKTiep > 0) bcThueXNK.GhiChu = "Chuyển lần sau TK.";
                            bcThueXNKCollectin.Add(bcThueXNK);
                            soToKhaiXuat = bcThueXNK.SoToKhaiXuat;
                            ngayDangKyXuat = bcThueXNK.NgayDangKyXuat;
                            soToKhaiNhap = bcThueXNK.SoToKhaiNhap;
                            maNPL = bcThueXNK.MaNPL;
                            ngayDangKyNhap = bc.NgayDangKyNhap;
                        }

                    }
                    this.InsertBCThueXNK(bcThueXNKCollectin, transaction);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
            return ds;
        }
        
        private DateTime GetNgayThucXuat(int soToKhaiXuat, DateTime ngayDangKyXuat)
        {
            this.LoadBKCollection();
            this.BKCollection[this.getBKToKhaiXuat()].LoadChiTietBangKe();
            BKToKhaiXuatCollection bks = this.BKCollection[this.getBKToKhaiXuat()].bkTKXColletion;
            foreach (BKToKhaiXuat bk in bks)
                if (soToKhaiXuat == bk.SoToKhai && ngayDangKyXuat == bk.NgayDangKy) return bk.NgayThucXuat;
            return new DateTime(1900,1,1);

        }
        private DataRow GetDonGia_TyGia_ThueSuat_ThueNK(DataTable dtNPLNhapTon, int SoToKhaiNhap, DateTime ngayDangKyNhap, string maNPL)
        {
            foreach (DataRow dr in dtNPLNhapTon.Rows)
                if (SoToKhaiNhap == Convert.ToInt32(dr["SoToKhai"]) && ngayDangKyNhap == Convert.ToDateTime(dr["NgayDangKy"]) && maNPL == dr["MaNPL"].ToString()) return dr;
            return null;
        }
        
        private void InsertBCXNT(BCXuatNhapTonCollection bcCollection,SqlTransaction transaction)
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            try
            {
                BCXuatNhapTon bc = new BCXuatNhapTon();
                bc.DeleteAllTransaction(transaction);
                bc.InsertTransaction(transaction, bcCollection);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        private void InsertBCThueXNK(BCThueXNKCollection bcCollection, SqlTransaction transaction)
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            try
            {
                BCThueXNK bc = new BCThueXNK();
                bc.DeleteAllTransaction(transaction);
                bc.InsertTransaction(transaction, bcCollection);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }
        #region WS

        public HoSoThanhLyDangKyInfo ExportToInfo()
        {
            HoSoThanhLyDangKyInfo hstlInfo = new HoSoThanhLyDangKyInfo();
            hstlInfo.Ma_DVKB = this.MaDoanhNghiep;
            hstlInfo.MaDoanhNghiep = this.MaDoanhNghiep;
            hstlInfo.MaHaiQuan = this.MaHaiQuanTiepNhan;
            hstlInfo.NamTiepNhan = this.NamTiepNhan;
            hstlInfo.NgayTiepNhan = this.NgayTiepNhan;
            hstlInfo.SoTiepNhan = this.SoTiepNhan;
            hstlInfo.TrangThaiXL = this.TrangThaiXuLy;
            if (this.BKCollection == null || this._BKCollection.Count == 0)
                this.BKCollection = BangKeHoSoThanhLy.SelectCollectionBy_MaterID(this.ID);

            hstlInfo.HoSoThanhLyCollection = new HoSoThanhLyInfo[this.BKCollection.Count];
            int i = 0;
            foreach (BangKeHoSoThanhLy bk in this.BKCollection)
            {

                #region  cap nhat ho dang ky detail
                HoSoThanhLyInfo hs = new HoSoThanhLyInfo();
                hs.MaBangKe = bk.MaBangKe;
                hs.MaDoanhNghiep = this.MaDoanhNghiep;
                hs.MaHaiQuan = this.MaHaiQuanTiepNhan;
                hs.MaHaiQuanTiepNhanBangKe = this.MaHaiQuanTiepNhan;
                hs.NamTiepNhan = this.NamTiepNhan;
                hs.NamTiepNhanBangKe = this.NamTiepNhan;
                hs.SoTiepNhan = this.SoTiepNhan;
                hs.SoTiepNhanBangKe = this.SoTiepNhan;
                hs.STTBangKe = bk.STTHang;
                hs.TenBangKe = bk.MaBangKe;
                hstlInfo.HoSoThanhLyCollection[i++] = hs;
                #endregion
                switch (bk.MaBangKe.Trim())
                {
                    case "DTLNPLCHUATL":
                        {
                            #region bảng kê nguyên phụ liệu chưa thanh lý
                            if (bk.bkNPLCTLCollection == null || bk.bkNPLCTLCollection.Count == 0)
                                bk.bkNPLCTLCollection = BKNPLChuaThanhLy.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLChuaThanhLyDangKyInfo info = new BKNPLChuaThanhLyDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLChuaThanhLyInFo[bk.bkNPLCTLCollection.Count];
                            int chuaTLIndex = 0;
                            foreach (BKNPLChuaThanhLy bkChuaThanhLy in bk.bkNPLCTLCollection)
                            {
                                BKNPLChuaThanhLyInFo entity = new BKNPLChuaThanhLyInFo();
                                entity.LUONG = bkChuaThanhLy.Luong;
                                entity.MA_DVT = bkChuaThanhLy.DVT_ID;
                                entity.MA_NPL = bkChuaThanhLy.MaNPL;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanTK = bkChuaThanhLy.MaHaiQuan;
                                entity.MaloaiHinh = bkChuaThanhLy.MaLoaiHinh;
                                entity.NamDangKy = bkChuaThanhLy.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkChuaThanhLy.NgayDangKy;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkChuaThanhLy.SoToKhai;
                                entity.STTHang = bkChuaThanhLy.STTHang;
                                entity.Ten_DVT = bkChuaThanhLy.TenDVT;
                                entity.TEN_NPL = bkChuaThanhLy.TenNPL;
                                info.NPLCollection[chuaTLIndex++] = entity;
                            }
                            hstlInfo.BKChuaThanhLy = info;
                            #endregion
                        }
                        break;
                    case "DTLCHITIETNT":
                        {
                            #region bảng kê nguyên phụ liệu tạm nộp thuế
                            if (bk.bkNPLTNTCollection == null || bk.bkNPLTNTCollection.Count == 0)
                                bk.bkNPLTNTCollection = BKNPLTamNopThue.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKTamNopThueDangKyInfo info = new BKTamNopThueDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKTamNopThueInfo[bk.bkNPLTNTCollection.Count];
                            int tamNopThueIndex = 0;
                            foreach (BKNPLTamNopThue bkTamNopThue in bk.bkNPLTNTCollection)
                            {
                                BKTamNopThueInfo entity = new BKTamNopThueInfo();
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanTK = bkTamNopThue.MaHaiQuan;
                                entity.MaloaiHinh = bkTamNopThue.MaLoaiHinh;
                                entity.MaNPL = bkTamNopThue.MaNPL;
                                entity.NamDangKy = bkTamNopThue.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NGAY_NT = bkTamNopThue.NgayNopThue;
                                entity.NOP_CLGIA = bkTamNopThue.Nop_CLGia;
                                entity.NOP_NK = bkTamNopThue.Nop_NK;
                                entity.NOP_TTDB = bkTamNopThue.Nop_TTDB;
                                entity.NOP_VAT = bkTamNopThue.Nop_VAT;
                                entity.SO_TBT_QDDC = bkTamNopThue.So_TBT_QDDC;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkTamNopThue.SoToKhai;
                                entity.STTHang = bkTamNopThue.STTHang;
                                entity.TenNPL = bkTamNopThue.TenNPL;
                                info.NPLCollection[tamNopThueIndex++] = entity;
                            }
                            hstlInfo.BKTamNopThue = info;
                            #endregion
                        }
                        break;
                    case "DTLNPLNKD":
                        {
                            #region bảng kê nguyên phụ liệu nhập kinh doanh
                            if (bk.bkNPLNKDCollection == null || bk.bkNPLNKDCollection.Count == 0)
                                bk.bkNPLNKDCollection = BKNPLXuatSuDungNKD.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLNhapKinhDoanhDangKyInFo info = new BKNPLNhapKinhDoanhDangKyInFo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLNhapKinhDoanhInfo[bk.bkNPLTNTCollection.Count];
                            int index = 0;
                            foreach (BKNPLXuatSuDungNKD bkDetail in bk.bkNPLNKDCollection)
                            {
                                BKNPLNhapKinhDoanhInfo entity = new BKNPLNhapKinhDoanhInfo();
                                entity.LUONG_SD = bkDetail.LuongSuDung;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.MA_NPL = bkDetail.MaNPL;
                                entity.MA_SP = bkDetail.MaSP;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanXuat = bkDetail.MaHaiQuanXuat;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.MaLoaiHinhXuat = bkDetail.MaLoaiHinhXuat;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamDangKyXuat = bkDetail.NamDangKyXuat;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.NgayDangKyXuat = bkDetail.NgayDangKyXuat;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.SoToKhaiXuat = bkDetail.SoToKhaiXuat;
                                entity.STTHang = bkDetail.STTHang;
                                info.NPLCollection[index++] = entity;
                            }
                            hstlInfo.BKNhapKinhDoanh = info;
                            #endregion
                        }
                        break;
                    case "DTLNPLNT":
                        {
                            #region bảng kê nguyên phụ liệu nộp thuế
                            if (bk.bkNPLNTCollection == null || bk.bkNPLNTCollection.Count == 0)
                                bk.bkNPLNTCollection = BKNPLNopThueTieuThuNoiDia.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLNopThueDangKyInfo info = new BKNPLNopThueDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLNopThueInfo[bk.bkNPLNTCollection.Count];
                            int index = 0;
                            foreach (BKNPLNopThueTieuThuNoiDia bkDetail in bk.bkNPLNTCollection)
                            {
                                BKNPLNopThueInfo entity = new BKNPLNopThueInfo();
                                entity.LUONG_NT = bkDetail.LuongNopThue;
                                entity.MA_DVT = bkDetail.DVT_ID;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanTK = bkDetail.MaHaiQuan;
                                entity.MaloaiHinh = bkDetail.MaLoaiHinh;
                                entity.MaNPL = bkDetail.MaNPL;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NGAYDK = bkDetail.NgayDangKy;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.STTHang = bkDetail.STTHang;
                                entity.Ten_DVT = bkDetail.TenDVT;
                                entity.TenNPL = bkDetail.TenNPL;
                                info.NPLCollection[index++] = entity;
                            }
                            hstlInfo.BKNopThue = info;
                            #endregion
                        }
                        break;
                    case "DTLNPLTX":
                        {
                            #region bảng kê nguyên phụ liệu tái xuất
                            if (bk.bkNPLTXCollection == null || bk.bkNPLTXCollection.Count == 0)
                                bk.bkNPLTXCollection = BKNPLTaiXuat.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLTaiXuatDangKyInfo info = new BKNPLTaiXuatDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLTaiXuatInfo[bk.bkNPLTXCollection.Count];
                            int index = 0;
                            foreach (BKNPLTaiXuat bkDetail in bk.bkNPLTXCollection)
                            {
                                BKNPLTaiXuatInfo entity = new BKNPLTaiXuatInfo();
                                entity.LUONGTX = bkDetail.LuongTaiXuat;
                                entity.MA_DVT = bkDetail.DVT_ID;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.Ma_NPL = bkDetail.MaNPL;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanTaiXuat = bkDetail.MaHaiQuanXuat;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.MaLoaiHinhXuat = bkDetail.MaLoaiHinhXuat;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamDangKyXuat = bkDetail.NamDangKyXuat;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.NgayDangKyXuat = bkDetail.NgayDangKyXuat;
                                //entity.NGAYTH=bkDetail.ngay
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.SoToKhaiXuat = bkDetail.SoToKhaiXuat;
                                entity.STTHang = bkDetail.STTHang;
                                entity.Ten_DVT = bkDetail.TenDVT;
                                entity.TEN_NPL = bkDetail.TenNPL;
                                info.NPLCollection[index++] = entity;
                            }
                            hstlInfo.BKTaiXuat = info;
                            #endregion
                        }
                        break;
                    case "DTLNPLXGC":
                        {
                            #region bảng kê nguyên phụ liệu xuất gia công
                            if (bk.bkNPLXGCCollection == null || bk.bkNPLXGCCollection.Count == 0)
                                bk.bkNPLXGCCollection = BKNPLXuatGiaCong.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLXuatGiaCongDangKyInfo info = new BKNPLXuatGiaCongDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLXuatGiaCongInfo[bk.bkNPLXGCCollection.Count];
                            int index = 0;
                            foreach (BKNPLXuatGiaCong bkDetail in bk.bkNPLXGCCollection)
                            {
                                BKNPLXuatGiaCongInfo entity = new BKNPLXuatGiaCongInfo();
                                entity.LUONG_XGC = bkDetail.LuongXuat;
                                entity.MA_DVT = bkDetail.DVT_ID;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaHaiQuanXuat = bkDetail.MaHaiQuanXuat;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.MaLoaiHinhXuat = bkDetail.MaLoaiHinhXuat;
                                entity.MaNPL = bkDetail.MaNPL;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamDangKyXuat = bkDetail.NamDangKyXuat;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.NgayDangKyXuat = bkDetail.NgayDangKyXuat;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.SoToKhaiXuat = bkDetail.SoToKhaiXuat;
                                entity.STTHang = bkDetail.STTHang;
                                entity.Ten_DVT = bkDetail.TenDVT;
                                entity.TenNPL = bkDetail.TenNPL;
                                info.NPLCollection[index++] = entity;
                            }
                            hstlInfo.BKXuatGiaCong = info;
                            #endregion
                        }
                        break;
                    case "DTLNPLXH":
                        {
                            #region bảng kê nguyên phụ liệu xin hủy
                            if (bk.bkNPLXHCollection == null || bk.bkNPLXHCollection.Count == 0)
                                bk.bkNPLXHCollection = BKNPLXinHuy.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKNPLHuyBieuTangDangKyInfo info = new BKNPLHuyBieuTangDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.NPLCollection = new BKNPLHuyBieuTangInfo[bk.bkNPLXHCollection.Count];
                            int index = 0;
                            foreach (BKNPLXinHuy bkDetail in bk.bkNPLXHCollection)
                            {
                                BKNPLHuyBieuTangInfo entity = new BKNPLHuyBieuTangInfo();
                                entity.BienBanHuy = bkDetail.BienBanHuy;
                                entity.LuongHuy = bkDetail.LuongHuy;
                                entity.MA_DVT = bkDetail.DVT_ID;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.MaNPL = bkDetail.MaNPL;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.NgayHuy = bkDetail.NgayHuy;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.STTHang = bkDetail.STTHang;
                                entity.Ten_DVT = bkDetail.TenDVT;
                                entity.TenNPL = bkDetail.TenNPL;
                                info.NPLCollection[index++] = entity;
                            }
                            hstlInfo.BKHuyBieuTang = info;
                            #endregion
                        }
                        break; ;
                    case "DTLTKN":
                        {
                            #region bảng kê tờ khai nhập
                            if (bk.bkTKNCollection == null || bk.bkTKNCollection.Count == 0)
                                bk.bkTKNCollection = BKToKhaiNhap.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKToKhaiNhapDangKyInfo info = new BKToKhaiNhapDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.ToKhaiCollection = new BKToKhaiNhapInfo[bk.bkTKNCollection.Count];
                            int index = 0;
                            foreach (BKToKhaiNhap bkDetail in bk.bkTKNCollection)
                            {
                                BKToKhaiNhapInfo entity = new BKToKhaiNhapInfo();
                                entity.GHICHU = bkDetail.GhiChu;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.STTHang = bkDetail.STTHang;
                                entity.NGAYTHN = bkDetail.NgayThucNhap;
                                info.ToKhaiCollection[index++] = entity;
                            }
                            hstlInfo.BKToKhaiNhap = info;
                            #endregion
                        }
                        break;
                    case "DTLTKX":
                        {
                            #region bảng kê tờ khai xuất
                            if (bk.bkTKXColletion == null || bk.bkTKXColletion.Count == 0)
                                bk.bkTKXColletion = BKToKhaiXuat.SelectCollectionBy_BangKeHoSoThanhLy_ID(bk.ID);
                            BKToKhaiXuatDangKyInfo info = new BKToKhaiXuatDangKyInfo();
                            info.Ngay_TNCT = new DateTime(1900, 1, 1);
                            info.Ma_DVKB = this.MaDoanhNghiep;
                            info.MaDoanhNghiep = this.MaDoanhNghiep;
                            info.NamTiepNhan = this.NamTiepNhan;
                            info.NgayTiepNhan = this.NgayTiepNhan;
                            info.SoHoSoThanhLy = bk.SoHSTL;
                            info.SoTiepNhan = this.SoTiepNhan;
                            info.TrangThaiXL = bk.TrangThaiXL;
                            info.MaHaiQuan = this.MaHaiQuanTiepNhan;
                            info.ToKhaiCollection = new BKToKhaiXuatInfo[bk.bkTKXColletion.Count];
                            int index = 0;
                            foreach (BKToKhaiXuat bkDetail in bk.bkTKXColletion)
                            {
                                BKToKhaiXuatInfo entity = new BKToKhaiXuatInfo();
                                entity.GHICHU = bkDetail.GhiChu;
                                entity.Ma_HQ = bkDetail.MaHaiQuan;
                                entity.MaHaiQuan = this.MaHaiQuanTiepNhan;
                                entity.MaLoaiHinh = bkDetail.MaLoaiHinh;
                                entity.NamDangKy = bkDetail.NamDangKy;
                                entity.NamTiepNhan = this.NamTiepNhan;
                                entity.NgayDangKy = bkDetail.NgayDangKy;
                                entity.SoTiepNhan = this.SoTiepNhan;
                                entity.SoToKhai = bkDetail.SoToKhai;
                                entity.STTHang = bkDetail.STTHang;
                                entity.NGAYTHX = bkDetail.NgayThucXuat;
                                info.ToKhaiCollection[index++] = entity;
                            }
                            hstlInfo.BKToKhaiXuat = info;
                            #endregion
                        }
                        break;
                }
            }
            return hstlInfo;
        }

        public void WSSend()
        {
            KDT_SXXK_Service kdt = new KDT_SXXK_Service();
            this.SoTiepNhan = kdt.ThanhKhoan_Send(ExportToInfo());
            this.NgayTiepNhan = DateTime.Today;
            this.NamTiepNhan = (short)this.NgayTiepNhan.Year;
            this.TrangThaiXuLy = BLL.TrangThaiXuLy.CHO_DUYET;
            this.InsertUpdate();
        }

        public void WSCancel()
        {
            KDT_SXXK_Service kdt = new KDT_SXXK_Service();
            kdt.ThanhKhoan_Cancel(this.SoTiepNhan, this.MaHaiQuanTiepNhan, this.NamTiepNhan);
            this.NgayTiepNhan = new DateTime(1900, 1, 1);
            this.NamTiepNhan = 0;
            this.TrangThaiXuLy = BLL.TrangThaiXuLy.CHUA_KHAI_BAO;
            this.SoTiepNhan = 0;
            this.InsertUpdate();
        }

        public void WSRequest()
        {
            KDT_SXXK_Service kdt = new KDT_SXXK_Service();
            HoSoThanhLyDangKyInfo hstl = kdt.ThanhKhoan_RequestStatus(this.SoTiepNhan, this.MaHaiQuanTiepNhan, this.NamTiepNhan);
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    this.TrangThaiXuLy = hstl.TrangThaiXL;
                    BLL.SXXK.ThanhKhoan.LanThanhLyBase lanthanhly = new Company.BLL.SXXK.ThanhKhoan.LanThanhLyBase();                        
                    if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                    {
                        lanthanhly.LanThanhLy = hstl.LanThanhLy;
                        lanthanhly.MaDoanhNghiep = hstl.MaDoanhNghiep;
                        lanthanhly.MaHaiQuan = hstl.MaHaiQuan;
                        lanthanhly.NgayBatDau = hstl.NgayBatDauTL;
                        lanthanhly.NamThanhLy = (short)hstl.NgayBatDauTL.Year;
                        lanthanhly.SoHoSo = hstl.SoHoSoThanhLy;
                        lanthanhly.TrangThai = hstl.TrangThaiCuaHSBenSXXK;
                        lanthanhly.InsertUpdateTransaction(transaction);
                    }
                    if (this.BKCollection == null || this.BKCollection.Count == 0)
                        LoadBKCollection();
                    BangKeHoSoThanhLy bangkeDelete = new BangKeHoSoThanhLy();
                    bangkeDelete.DeleteCollection(this.BKCollection, transaction);
                    this.BKCollection.Clear();

                    #region  Bảng kê tờ khai nhập
                    if (hstl.BKToKhaiNhap != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLTKN";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê Tờ khai nhập khẩu thanh lý ";
                        bk.TrangThaiXL = hstl.BKToKhaiNhap.TrangThaiXL;
                        bk.bkTKNCollection = new BKToKhaiNhapCollection();
                        foreach (BKToKhaiNhapInfo info in hstl.BKToKhaiNhap.ToKhaiCollection)
                        {
                            BKToKhaiNhap entity = new BKToKhaiNhap();
                            entity.GhiChu = info.GHICHU;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.NgayThucNhap = info.NGAYTHN;
                            bk.bkTKNCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.ToKhaiNhap tkNhap = new Company.BLL.SXXK.ThanhKhoan.ToKhaiNhap();
                                tkNhap.LanThanhLy = lanthanhly.LanThanhLy;
                                tkNhap.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                tkNhap.MaHaiQuan = entity.MaHaiQuan;
                                tkNhap.MaLoaiHinh = entity.MaLoaiHinh;
                                tkNhap.NamDangKy = entity.NamDangKy;
                                tkNhap.SoToKhai = entity.SoToKhai;
                                if (!tkNhap.Load())
                                    tkNhap.InsertTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion

                    #region  Bảng kê tờ khai xuất
                    if (hstl.BKToKhaiXuat != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLTKX";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê Tờ khai xuất khẩu thanh lý ";
                        bk.TrangThaiXL = hstl.BKToKhaiXuat.TrangThaiXL;
                        bk.bkTKXColletion = new BKToKhaiXuatCollection();
                        foreach (BKToKhaiXuatInfo info in hstl.BKToKhaiXuat.ToKhaiCollection)
                        {
                            BKToKhaiXuat entity = new BKToKhaiXuat();
                            entity.GhiChu = info.GHICHU;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.NgayThucXuat = info.NGAYTHX;
                            bk.bkTKXColletion.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.ToKhaiXuat tkXuat = new Company.BLL.SXXK.ThanhKhoan.ToKhaiXuat();
                                tkXuat.LanThanhLy = lanthanhly.LanThanhLy;
                                tkXuat.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                tkXuat.MaHaiQuan = lanthanhly.MaHaiQuan;
                                tkXuat.MaLoaiHinh = entity.MaLoaiHinh;
                                tkXuat.NamDangKy = entity.NamDangKy;
                                tkXuat.SoToKhai = entity.SoToKhai;
                                tkXuat.NgayThucXuat = entity.NgayThucXuat;
                                tkXuat.MaHaiQuanXuat = info.Ma_HQ;
                                if (!tkXuat.Load())
                                    tkXuat.InsertTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion
                    #region  Bảng kê chưa thanh lý
                    if (hstl.BKChuaThanhLy != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLCHUATL";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;                        
                        bk.TenBangKe = "Bảng kê chi tiết lượng NPL sử dụng chưa thanh lý";
                        bk.TrangThaiXL = hstl.BKChuaThanhLy.TrangThaiXL;
                        bk.bkNPLCTLCollection = new BKNPLChuaThanhLyCollection();
                        foreach (BKNPLChuaThanhLyInFo info in hstl.BKChuaThanhLy.NPLCollection)
                        {
                            BKNPLChuaThanhLy entity = new BKNPLChuaThanhLy();
                            entity.Luong = info.LUONG;
                            entity.DVT_ID = info.MA_DVT;
                            entity.MaNPL = info.MA_NPL;
                            entity.MaHaiQuan = info.MaHaiQuanTK;
                            entity.MaLoaiHinh = info.MaloaiHinh;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.TenDVT = info.Ten_DVT;
                            entity.TenNPL = info.TEN_NPL;
                            bk.bkNPLCTLCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLChuaThanhLy nplChuaThanhLy = new Company.BLL.SXXK.ThanhKhoan.NPLChuaThanhLy();
                                nplChuaThanhLy.LanThanhLy = lanthanhly.LanThanhLy;
                                nplChuaThanhLy.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplChuaThanhLy.MaHaiQuan = entity.MaHaiQuan;
                                nplChuaThanhLy.MaLoaiHinh = entity.MaLoaiHinh;
                                nplChuaThanhLy.MaNPL = entity.MaNPL;
                                nplChuaThanhLy.NamDangKy = entity.NamDangKy;
                                nplChuaThanhLy.SoLuong = entity.Luong;
                                nplChuaThanhLy.SoToKhai = entity.SoToKhai;
                                nplChuaThanhLy.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion  Bảng kê chưa thanh lý

                    #region  Bảng kê hủy biếu tặng
                    if (hstl.BKHuyBieuTang != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLXH";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê NPL huỷ, biếu tặng";
                        bk.TrangThaiXL = hstl.BKHuyBieuTang.TrangThaiXL;
                        bk.bkNPLXHCollection = new BKNPLXinHuyCollection();
                        foreach (BKNPLHuyBieuTangInfo info in hstl.BKHuyBieuTang.NPLCollection)
                        {
                            BKNPLXinHuy entity = new BKNPLXinHuy();
                            entity.BienBanHuy = info.BienBanHuy;
                            entity.LuongHuy = info.LuongHuy;
                            entity.DVT_ID = info.MA_DVT;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.MaNPL = info.MaNPL;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.NgayHuy = info.NgayHuy;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.TenDVT = info.Ten_DVT;
                            entity.TenNPL = info.TenNPL;
                            bk.bkNPLXHCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLHuy nplHuy = new Company.BLL.SXXK.ThanhKhoan.NPLHuy();
                                nplHuy.BienBanHuy = entity.BienBanHuy;
                                nplHuy.LanThanhLy = lanthanhly.LanThanhLy;
                                nplHuy.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplHuy.MaHaiQuan = entity.MaHaiQuan;
                                nplHuy.MaLoaiHinh = entity.MaLoaiHinh;
                                nplHuy.MaNPL = entity.MaNPL;
                                nplHuy.NamDangKy = entity.NamDangKy;
                                nplHuy.NgayHuy = entity.NgayHuy;
                                nplHuy.SoLuongHuy = entity.LuongHuy;
                                nplHuy.SoToKhai = entity.SoToKhai;
                                nplHuy.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion  Bảng kê chưa thanh lý

                    #region  Bảng kê nhập kinh doanh
                    if (hstl.BKNhapKinhDoanh != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLNKD";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê NPL xuất sử dụng tờ khai NKD";
                        bk.TrangThaiXL = hstl.BKNhapKinhDoanh.TrangThaiXL;
                        bk.bkNPLNKDCollection = new BKNPLXuatSuDungNKDCollection();
                        foreach (BKNPLNhapKinhDoanhInfo info in hstl.BKNhapKinhDoanh.NPLCollection)
                        {
                            BKNPLXuatSuDungNKD entity = new BKNPLXuatSuDungNKD();
                            entity.LuongSuDung = info.LUONG_SD;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaNPL = info.MA_NPL;
                            entity.MaSP = info.MA_SP;
                            entity.MaHaiQuanXuat = info.MaHaiQuanXuat;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.MaLoaiHinhXuat = info.MaLoaiHinhXuat;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NamDangKyXuat = info.NamDangKyXuat;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.NgayDangKyXuat = info.NgayDangKyXuat;
                            entity.SoToKhai = info.SoToKhai;
                            entity.SoToKhaiXuat = info.SoToKhaiXuat;
                            entity.STTHang = info.STTHang;
                            bk.bkNPLNKDCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLXuatNKD nplNKD = new Company.BLL.SXXK.ThanhKhoan.NPLXuatNKD();
                                nplNKD.LanThanhLy = lanthanhly.LanThanhLy;
                                nplNKD.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplNKD.MaHaiQuan = entity.MaHaiQuan;
                                nplNKD.MaHaiQuanXuat = entity.MaHaiQuanXuat;
                                nplNKD.MaLoaiHinh = entity.MaLoaiHinh;
                                nplNKD.MaLoaiHinhXuat = entity.MaLoaiHinhXuat;
                                nplNKD.MaNPL = entity.MaNPL;
                                nplNKD.MaSP = entity.MaSP;
                                nplNKD.NamDangKy = entity.NamDangKy;
                                nplNKD.NamDangKyXuat = entity.NamDangKyXuat;
                                nplNKD.SoLuongSuDung = entity.LuongSuDung;
                                nplNKD.SoToKhai = entity.SoToKhai;
                                nplNKD.SoToKhaiXuat = entity.SoToKhaiXuat;
                                nplNKD.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion  Bảng kê chưa thanh lý

                    #region  Bảng kê NPL nộp thuế
                    if (hstl.BKNopThue != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLNT";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê NPL nộp thuế tiêu thụ nội địa ";
                        bk.TrangThaiXL = hstl.BKNopThue.TrangThaiXL;
                        bk.bkNPLNTCollection = new BKNPLNopThueTieuThuNoiDiaCollection();
                        foreach (BKNPLNopThueInfo info in hstl.BKNopThue.NPLCollection)
                        {
                            BKNPLNopThueTieuThuNoiDia entity = new BKNPLNopThueTieuThuNoiDia();
                            entity.LuongNopThue = info.LUONG_NT;
                            entity.DVT_ID = info.MA_DVT;
                            entity.MaHaiQuan = info.MaHaiQuanTK;
                            entity.MaLoaiHinh = info.MaloaiHinh;
                            entity.MaNPL = info.MaNPL;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayDangKy = info.NGAYDK;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.TenDVT = info.Ten_DVT;
                            entity.TenNPL = info.TenNPL;
                            bk.bkNPLNTCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLNopThue nplNopThue = new Company.BLL.SXXK.ThanhKhoan.NPLNopThue();
                                nplNopThue.LanThanhLy = lanthanhly.LanThanhLy;
                                nplNopThue.LuongNopThue = entity.LuongNopThue;
                                nplNopThue.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplNopThue.MaHaiQuan = entity.MaHaiQuan;
                                nplNopThue.MaLoaiHinh = entity.MaLoaiHinh;
                                nplNopThue.MaNPL = entity.MaNPL;
                                nplNopThue.NamDangKy = entity.NamDangKy;
                                nplNopThue.SoToKhai = entity.SoToKhai;
                                nplNopThue.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion

                    #region  Bảng kê NPL tái xuất
                    if (hstl.BKTaiXuat != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLTX";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê NPL tái xuất ";
                        bk.TrangThaiXL = hstl.BKTaiXuat.TrangThaiXL;
                        bk.bkNPLTXCollection = new BKNPLTaiXuatCollection();
                        foreach (BKNPLTaiXuatInfo info in hstl.BKTaiXuat.NPLCollection)
                        {
                            BKNPLTaiXuat entity = new BKNPLTaiXuat();
                            entity.LuongTaiXuat = info.LUONGTX;
                            entity.DVT_ID = info.MA_DVT;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaNPL = info.Ma_NPL;
                            entity.MaHaiQuanXuat = info.MaHaiQuanTaiXuat;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.MaLoaiHinhXuat = info.MaLoaiHinhXuat;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NamDangKyXuat = info.NamDangKyXuat;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.NgayDangKyXuat = info.NgayDangKyXuat;
                            entity.SoToKhai = info.SoToKhai;
                            entity.SoToKhaiXuat = info.SoToKhaiXuat;
                            entity.STTHang = info.STTHang;
                            entity.TenDVT = info.Ten_DVT;
                            entity.TenNPL = info.TEN_NPL;
                            bk.bkNPLTXCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLTaiXuat nplTaiXuat = new Company.BLL.SXXK.ThanhKhoan.NPLTaiXuat();
                                nplTaiXuat.LanThanhLy = lanthanhly.LanThanhLy;
                                nplTaiXuat.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplTaiXuat.MaHaiQuan = entity.MaHaiQuan;
                                nplTaiXuat.MaHaiQuanXuat = entity.MaHaiQuanXuat;
                                nplTaiXuat.MaLoaiHinh = entity.MaLoaiHinh;
                                nplTaiXuat.MaLoaiHinhXuat = entity.MaLoaiHinhXuat;
                                nplTaiXuat.MaNPL = entity.MaNPL;
                                nplTaiXuat.NamDangKy = entity.NamDangKy;
                                nplTaiXuat.NamDangKyXuat = entity.NamDangKyXuat;
                                nplTaiXuat.SoLuongTaiXuat = entity.LuongTaiXuat;
                                nplTaiXuat.SoToKhai = entity.SoToKhai;
                                nplTaiXuat.SoToKhaiXuat = entity.SoToKhaiXuat;
                                nplTaiXuat.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion

                    #region  Bảng kê NPL tạm nộp thuế
                    if (hstl.BKTamNopThue != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLCHITIETNT";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "Bảng kê NPL tạm nộp thuế ";
                        bk.TrangThaiXL = hstl.BKTamNopThue.TrangThaiXL;
                        bk.bkNPLTNTCollection = new BKNPLTamNopThueCollection();
                        foreach (BKTamNopThueInfo info in hstl.BKTamNopThue.NPLCollection)
                        {
                            BKNPLTamNopThue entity = new BKNPLTamNopThue();
                            entity.MaHaiQuan = info.MaHaiQuanTK;
                            entity.MaLoaiHinh = info.MaloaiHinh;
                            entity.MaNPL = info.MaNPL;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NgayNopThue = info.NGAY_NT;
                            entity.Nop_CLGia = info.NOP_CLGIA;
                            entity.Nop_NK = info.NOP_NK;
                            entity.Nop_TTDB = info.NOP_TTDB;
                            entity.Nop_VAT = info.NOP_VAT;
                            entity.So_TBT_QDDC = info.SO_TBT_QDDC;
                            entity.SoToKhai = info.SoToKhai;
                            entity.STTHang = info.STTHang;
                            entity.TenNPL = info.TenNPL;
                            bk.bkNPLTNTCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLTamNopThue nplTamNopThue = new Company.BLL.SXXK.ThanhKhoan.NPLTamNopThue();
                                nplTamNopThue.NopCLGia = entity.Nop_CLGia;
                                nplTamNopThue.NopNK = entity.Nop_NK;
                                nplTamNopThue.NopTTDB = entity.Nop_TTDB;
                                nplTamNopThue.NopVAT = entity.Nop_VAT;
                                nplTamNopThue.LanThanhLy = lanthanhly.LanThanhLy;
                                nplTamNopThue.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplTamNopThue.MaHaiQuan = entity.MaHaiQuan;
                                nplTamNopThue.MaLoaiHinh = entity.MaLoaiHinh;
                                nplTamNopThue.MaNPL = entity.MaNPL;
                                nplTamNopThue.NamDangKy = entity.NamDangKy;
                                nplTamNopThue.NgayNopThue = entity.NgayNopThue;
                                nplTamNopThue.SoToKhai = entity.SoToKhai;
                                nplTamNopThue.InsertUpdateTransaction(transaction);
                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion

                

                    #region  Bảng kê tờ khai xuất gia công
                    if (hstl.BKXuatGiaCong != null)
                    {
                        BangKeHoSoThanhLy bk = new BangKeHoSoThanhLy();
                        bk.MaBangKe = "DTLNPLXGC";
                        bk.MaterID = this.ID;
                        bk.SoHSTL = hstl.SoHoSoThanhLy;
                        bk.TenBangKe = "  Bảng kê NPL xuất theo loại hình XGC";
                        bk.TrangThaiXL = hstl.BKXuatGiaCong.TrangThaiXL;
                        bk.bkNPLXGCCollection = new BKNPLXuatGiaCongCollection();
                        foreach (BKNPLXuatGiaCongInfo info in hstl.BKXuatGiaCong.NPLCollection)
                        {
                            BKNPLXuatGiaCong entity = new BKNPLXuatGiaCong();
                            entity.LuongXuat = info.LUONG_XGC;
                            entity.DVT_ID = info.MA_DVT;
                            entity.MaHaiQuan = info.Ma_HQ;
                            entity.MaHaiQuanXuat = info.MaHaiQuanXuat;
                            entity.MaLoaiHinh = info.MaLoaiHinh;
                            entity.MaLoaiHinhXuat = info.MaLoaiHinhXuat;
                            entity.MaNPL = info.MaNPL;
                            entity.NamDangKy = info.NamDangKy;
                            entity.NamDangKyXuat = info.NamDangKyXuat;
                            entity.NgayDangKy = info.NgayDangKy;
                            entity.NgayDangKyXuat = info.NgayDangKyXuat;
                            entity.SoToKhai = info.SoToKhai;
                            entity.SoToKhaiXuat = info.SoToKhaiXuat;
                            entity.STTHang = info.STTHang;
                            entity.TenDVT = info.Ten_DVT;
                            entity.TenNPL = info.TenNPL;
                            bk.bkNPLXGCCollection.Add(entity);
                            if (TrangThaiXuLy == BLL.TrangThaiXuLy.DA_DUYET)
                            {
                                BLL.SXXK.ThanhKhoan.NPLXGC nplXGC = new Company.BLL.SXXK.ThanhKhoan.NPLXGC();
                                nplXGC.LanThanhLy = lanthanhly.LanThanhLy;
                                nplXGC.MaDoanhNghiep = lanthanhly.MaDoanhNghiep;
                                nplXGC.MaHaiQuan = entity.MaHaiQuan;
                                nplXGC.MaHaiQuanXuat = entity.MaHaiQuanXuat;
                                nplXGC.MaLoaiHinh = entity.MaLoaiHinh;
                                nplXGC.MaLoaiHinhXuat = entity.MaLoaiHinhXuat;
                                nplXGC.MaNPL = entity.MaNPL;
                                nplXGC.NamDangKy = entity.NamDangKy;
                                nplXGC.NamDangKyXuat = entity.NamDangKyXuat;
                                nplXGC.SoLuongXuat = entity.LuongXuat;
                                nplXGC.SoToKhai = entity.SoToKhai;
                                nplXGC.SoToKhaiXuat = entity.SoToKhaiXuat;
                                nplXGC.InsertUpdateTransaction(transaction);

                            }
                        }
                        this.BKCollection.Add(bk);
                    }
                    #endregion
                    this.InsertUpdateFull(transaction);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
        #endregion WS
    }
}
